package com.pozu.new_retail.controllers;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pozu.new_retail.dao.model.Code;
import com.pozu.new_retail.dao.model.CodeSetting;
import com.pozu.new_retail.dao.base.JsonResult;
import com.pozu.new_retail.service.CodeService;
import com.pozu.new_retail.service.CodeSettingService;
import com.pozu.new_retail.service.OrderService;
import com.pozu.new_retail.utils.CodeConstants;
import com.pozu.new_retail.utils.CodeProperties;
import com.pozu.new_retail.utils.Tools;

@Controller
public class CodeController {
	
	@Autowired
	private CodeService codeService;
	@Autowired
	private OrderService orderService;
	@Autowired
	private CodeSettingService codeSettingService;
	
	@ResponseBody
	@RequestMapping(value = "/api/codes", method = RequestMethod.POST)
	public Object addCode(HttpServletRequest request) throws Exception{
		
		int appId = Integer.parseInt(request.getHeader("appId"));//那个小程序
		
		long userId = Long.parseLong(request.getHeader("userId"));
		
		Map<String,Object> map = Tools.getRequestData(request);
		
		String nickName = map.get("nickName").toString();//微信昵称
		//先判断是否有组团码
		Code codeObj = codeService.queryCodeObjByUserId(appId, userId);
		
		int state = 0;
		
		if (null == codeObj) {
			
			//此处判断是否使用过超级组团码购买过商品
			//String referCode = orderService.queryCodeByUserId(userId);
			
			codeObj = new Code();
			codeObj.setAppId(appId);
			codeObj.setUserId(userId);		
			//codeObj.setReferCode(referCode);
			codeObj.setLevel(0);
			
			//直到不重复为止
			while (state == 0) {
				
				nickName = nickName.replaceAll("[^a-zA-Z0-9_\u4e00-\u9fa5]", "");
				
				if (nickName.length() > 6) {
					nickName = nickName.substring(0, 6);
				}
				
				codeObj.setCode(nickName+Tools.getRandomCode(2));	
				state = codeService.saveCode(codeObj);
			}
			
		}
		
		JsonResult jr = new JsonResult();
		jr.respCode = CodeConstants.SUCCESS_CODE;
		jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);
		jr.respData = codeObj.getCode()+","+codeObj.getLevel();//组团码,等级(0普通组团码1微代理组团码2超级组团码)
		return jr;		
	}

	/**
	 * 升级为微代理组团码
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/api/codes", method = RequestMethod.PUT)
	public Object getUpdatedCode(HttpServletRequest request) throws Exception{

		int appId = request.getHeader("appId")==null?1:Integer.parseInt(request.getHeader("appId"));//那个小程序
		
		long userId = Long.parseLong(request.getHeader("userId"));

		HashMap<String, Object> map = new HashMap<String, Object>();

		map.put("userId", userId);
		map.put("level", 2);
		
		Code codeObj = codeService.queryCodeByMap(map);
		
		JsonResult jr = new JsonResult();
		
		//已有超级组团码
		if(null != codeObj){
			jr.respCode = CodeConstants.CJCODEEXISTED_MSG;
			jr.respMsg = CodeProperties.getString(CodeConstants.CJCODEEXISTED_MSG);
			return jr;
		}

		map = new HashMap<String, Object>();

		map.put("appId", appId);
		map.put("userId", userId);
		map.put("level", 3);
		
		codeObj = codeService.queryCodeByMap(map);
		
		//该小程序下已有总代理组团码
		if(null != codeObj){
			jr.respCode = CodeConstants.ZDLCODEEXISTED_MSG;
			jr.respMsg = CodeProperties.getString(CodeConstants.ZDLCODEEXISTED_MSG);
			return jr;
		}
		
		map = new HashMap<String, Object>();

		map.put("appId", appId);
		map.put("userId", userId);
		map.put("level", 1);
		
		codeObj = codeService.queryCodeByMap(map);
		
		//该小程序下已有微代理组团码
		if(null != codeObj){
			jr.respCode = CodeConstants.WDLCODEEXISTED_MSG;
			jr.respMsg = CodeProperties.getString(CodeConstants.WDLCODEEXISTED_MSG);
			return jr;
		}
			
		map = new HashMap<String, Object>();
		map.put("userId", userId);
		map.put("level", 0);
			
		codeObj = codeService.queryCodeByMap(map);
		
		//还没有普通组团码先申请普通的
		if (null == codeObj) {
			jr.respCode = CodeConstants.PTCODENOTEXISTED_MSG;
			jr.respMsg = CodeProperties.getString(CodeConstants.PTCODENOTEXISTED_MSG);
			return jr;
		}
		
		//判断是否满足升级条件
		
		map = new HashMap<String, Object>();
		map.put("type", 1);
		map.put("appId", appId);
		
		CodeSetting codeSetting = codeSettingService.queryBaseInfoByMap(map);
		
		int applyNumber = codeSetting.getApplyNumber();//申请微代理需要完成的单数
		
		map = new HashMap<String, Object>();
		map.put("code", codeObj.getCode());
		map.put("appId", appId);
		
		int orderNumber = orderService.queryOrderCountByMap(map);
		
		if(orderNumber < applyNumber){
			jr.respCode = CodeConstants.WDLCODEAPPLYERROR_MSG;
			jr.respMsg = CodeProperties.getString(CodeConstants.WDLCODEAPPLYERROR_MSG);
			return jr;
		}
		
		Code temp = new Code();
		temp.setUserId(userId);
		temp.setAppId(appId);
		temp.setCode(codeObj.getCode());
		temp.setReferCode(codeObj.getReferCode());
		temp.setLevel(1);
		codeService.saveCode(temp);

		jr.respCode = CodeConstants.SUCCESS_CODE;
		jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);
		jr.respData = temp.getCode()+","+temp.getLevel();//组团码,等级(0普通组团码1微代理组团码2超级组团码)

		return jr;
	}
	
	/**
	 * 检查组团码是否有效
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/api/codes", method = RequestMethod.GET)
	public Object checkWXOrder(HttpServletRequest request) throws Exception{
		
		int appId = Integer.parseInt(request.getHeader("appId"));//那个小程序
		
		long userId = Long.parseLong(request.getHeader("userId"));//那个小程序
		
		String code = request.getParameter("code");//组团码
	
		JsonResult jr = new JsonResult();

		Integer[] discount = codeService.queryDiscountByCode(appId, code,userId);
		
		if (null == discount) {
			jr.respCode = CodeConstants.CODENOTEXISTED_MSG;
			jr.respMsg = CodeProperties.getString(CodeConstants.CODENOTEXISTED_MSG);
			return jr;
		}
		
		jr.respCode = CodeConstants.SUCCESS_CODE;
		jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);
		
		jr.respData = discount[1];
		
		return jr;
	}

}
